<?php
namespace App\Services\Algo;
use App\Interf\Calculable;
use App\Models\LogHelper;
use TestingItemService;
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/11/24
 * Time: 15:52
 */

class TbWestMediAlgo extends BaseTestingAlgo implements Calculable{


    public function __construct($tid,$answerStr,$typeFourExpress=null)
    {
        parent::__construct($tid,$answerStr);
        $this->typeFourExpress = $typeFourExpress;

        $this->specialIndex = 34;
        //首先先组装所有的题目答案
        $this->convertAllAnswer();

    }

    /**R值接口
     * @return int
     */
    public function calculate()
    {
        return $this->entrance($this->answers);
    }


    /**将拼装后的答案转换成字符串
     * @return string
     */
    public function resultToStr(){
        return $this->arrToStr($this->answers);
    }


    /**
     *
     */
    protected function entrance($answers){
        $totalScore = 0;
        foreach ($answers as $answer){
            if($answer['index'] != $this->specialIndex ){
                $totalScore = $totalScore + $answer['weight'];
            }
        }
        $totalScore =  $totalScore+40;
        if($totalScore >= 100){
            $totalScore = 99;
        }
        if($totalScore <= 0){
            $totalScore = 1;
        }
        return $totalScore;
    }


    protected function convertTypeSix(){
        $types = 6;
        $dymTypeData = TestingItemService::getItemsByQuestionType($this->tid,$types,'_SIX');
        if(count($dymTypeData)>0){
            foreach ($dymTypeData as $data){
                $expressStr = $data['expression'];
                $weight = $this->getTypeSixSingleItemWeight($expressStr);
                $obj['index'] = $data['index'];
                $obj['answer'] = '';
                $obj['weight'] = $weight;
                array_push($this->answers,$obj);
            }
        }
    }



    public function convertAllAnswer(){
        //转换第四种表达式
        if($this->typeFourExpress){
            $fourTypeResult = $this->fourTypeArray($this->typeFourExpress,$this->answers);
            foreach($fourTypeResult as $v){
                array_push($this->answers,$v);
            }
        }

        //获取第六种表达式的权值
        $types = 6;
        $dymTypeData = TestingItemService::getItemsByQuestionType($this->tid,$types,'_SIX');
        if(count($dymTypeData)>0){
            foreach ($dymTypeData as $data){
                $expressStr = $data['expression'];
                $weight = $this->getTypeSixSingleItemWeight($expressStr);
                $obj['index'] = $data['index'];
                $obj['answer'] = '';
                $obj['weight'] = $weight;
                array_push($this->answers,$obj);
            }
        }
    }



}